/***************************************************************************
                          ContinentView.cpp  -  description
                             -------------------
    begin                : October 29 2009
    copyright            : Copyright 2009, ACKERMANN Thibaut
    email                : tackerma@edu.univ-fcomte.fr
    website              : http://code.google.com/p/nover-project/
    license              : GNU GPL v3
 ***************************************************************************/
/* *************************************************************************
 *  This file is part of NOver-Project.
 *  
 *  NOver-Project is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  NOver-Project is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with NOver-Project.  If not, see <http://www.gnu.org/licenses/>.
 * *************************************************************************/

#include "ContinentView.hpp"
#include "PlayState.hpp"

using namespace std;


ContinentView::ContinentView(std::string name) throw (MyException)
{	
	// on récupere quelques infos.
	gui = StateMgr::Instance()->getGuiEnv();
	driver = StateMgr::Instance()->getDriver();
	winheight = StateMgr::Instance()->getWindowHeight();
	winwidth = StateMgr::Instance()->getWindowWidth();
	
	
	// on récupere le Continent correspondant
	continent = DataMgr::Instance()->getContinentByName(name);
	
	if(!continent)
		throw MyException("Erreur, le continent n'a pas été chargé par le DataManager", __FILE__, __LINE__);
	
	carte = continent->map;
	
	////////////
	// la carte 
	
	zoomMap = new Zoomable(0, id_ContinentZoomable, core::rect<s32>(0, 0, winwidth-150, winheight));
	zoomMap->setImage(driver->getTexture(Path(MediaPath).a("Continents").a(continent->map).genc()));
	gui->getRootGUIElement()->addChild(zoomMap);
	
	
	/////////////////
	// nom de la vue
	textVueContinent = gui->addStaticText(L"Vue Continent", rect<s32>(winwidth-130, 0, winwidth, 20));
	
	textVueContinent->setOverrideFont(gui->getFont("../media/fonthaettenschweiler.bmp"));
	
	///////////////////
	// texte continent
	textChoixContinent = gui->addStaticText(L"Continent", rect<s32>(winwidth-150, 25, winwidth-100, 40));
	
	///////////////////////////////
	// combo de choix du continent
	comboChoixContinent = gui->addComboBox(rect<s32>(winwidth-100, 15, winwidth, 40), 0, id_ContinentCombo);
	

	PlayState* ps = dynamic_cast<PlayState*>(StateMgr::Instance()->getCurrentGameState());
	
	// oups !
	if(!ps)
		throw MyException("Erreur inattendue de dynamic_cast sur le PlayState", __FILE__, __LINE__);
	
	continentCount = DataMgr::Instance()->getContinentsCount();
	
	nomContinents = new wchar_t*[continentCount];
	
	for(int i = 0; i<continentCount; i++)
	{
		nomContinents[i] = stows(DataMgr::Instance()->getContinentAt(i)->name);
		comboChoixContinent->addItem(nomContinents[i]);
	}
}


ContinentView::~ContinentView()
{
	for(int i = 0; i<continentCount; i++)
	{
		delete[] nomContinents[i];
	}
	delete[] nomContinents;
	
	delete zoomMap;
}


void ContinentView::frame(float time)
{
	// TODO : afficher uniquement dans un mode "debug"
	unsigned int size = continent->Countries.size();
	for(unsigned int i = 0; i<size; i++)
	{
		Country* c = continent->Countries.at(i);
		
		int x1scr, y1scr, x2scr, y2scr;
		
		zoomMap->TextureToScreen(c->X1, c->Y1, &x1scr, &y1scr);
		zoomMap->TextureToScreen(c->X2, c->Y2, &x2scr, &y2scr);
		
		StateMgr::Instance()->getDriver()->draw2DRectangle(SColor(128, 255, 0, 0), rect<int>(x1scr, y1scr, x2scr, y2scr));
	}
	
	// barre de séparation entre la carte et le menu de droite.
	StateMgr::Instance()->getDriver()->draw2DLine(position2d<s32>(winwidth-150, 0), position2d<s32>(winwidth-150, winheight), SColor(255, 0, 0, 0));
}


void ContinentView::resize(int width, int height)
{
	// 150 de marge a droite, pour le menu
	zoomMap->setAbsolute(rect<int>(0, 0, width-150, height));
	
	comboChoixContinent->setRelativePosition(position2d<int>(width-100, 15));
	
	textVueContinent->setRelativePosition(position2d<int>(width-130, 0));
	
	textChoixContinent->setRelativePosition(position2d<int>(width-150, 25));

	winheight = height;
	winwidth = width;
}


std::string ContinentView::matchCountry(int x, int y)
{
	unsigned int size = continent->Countries.size();
	for(unsigned int i = 0; i<size; i++)
	{
		Country* c = continent->Countries.at(i);
		
		core::rect<int> area = core::rect<int>(c->X1, c->Y1, c->X2, c->Y2);
		
		int xscr, yscr;
		
		zoomMap->ScreenToTexture(x, y, &xscr, &yscr);
		
		if(area.isPointInside(position2d<s32>(xscr, yscr)))
			return c->name;
	}
	
	return "";
}


